package com.bincloud.vue.admin.web.service.ums.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bincloud.vue.admin.common.exception.BaseException;
import com.bincloud.vue.admin.common.exception.CodeMsg;
import com.bincloud.vue.admin.common.util.BaseEntityUtils;
import com.bincloud.vue.admin.common.util.ObjectUtils;
import com.bincloud.vue.admin.dao.mapper.ums.UmsRoleResourceRelationMapper;
import com.bincloud.vue.admin.model.entity.UmsResource;
import com.bincloud.vue.admin.model.entity.UmsRole;
import com.bincloud.vue.admin.model.entity.UmsRoleResourceRelation;
import com.bincloud.vue.admin.web.dto.ums.role.resource.UmsRoleResourceRelationReqDTO;
import com.bincloud.vue.admin.web.service.ums.IUmsResourceService;
import com.bincloud.vue.admin.web.service.ums.IUmsRoleResourceRelationService;
import com.bincloud.vue.admin.web.service.ums.IUmsRoleService;
import com.bincloud.vue.admin.web.util.request.RequestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bincloud, mvpzhou
 * @since 2022-02-17
 */
@Service
public class UmsRoleResourceRelationServiceImpl extends ServiceImpl<UmsRoleResourceRelationMapper, UmsRoleResourceRelation> implements IUmsRoleResourceRelationService {
    @Resource
    private IUmsRoleService umsRoleService;

    @Resource
    private IUmsResourceService umsResourceService;

    @Resource
    private RequestService requestService;

    /**
     * 角色资源关系绑定
     *
     * @param reqDTO {@link UmsRoleResourceRelationReqDTO}
     * @return 是否成功
     */
    @Override
    public boolean bind(UmsRoleResourceRelationReqDTO reqDTO) {
        // 获取角色信息
        UmsRole role = getRole(reqDTO);
        // 获取资源信息
        UmsResource resource = getResource(reqDTO);
        // 角色资源关系
        UmsRoleResourceRelation relation = getUmsRoleResourceRelation(role.getId(), resource.getId());
        if (ObjectUtils.isNotEmpty(relation)) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_EXISTED);
        }
        relation = new UmsRoleResourceRelation();
        relation.setRid(role.getId());
        relation.setSid(resource.getId());
        BaseEntityUtils.create(relation, requestService.getUsername());
        return this.save(relation);
    }

    /**
     * 角色资源关系解除绑定
     *
     * @param reqDTO {@link UmsRoleResourceRelationReqDTO}
     * @return 是否成功
     */
    @Override
    public boolean lift(UmsRoleResourceRelationReqDTO reqDTO) {
        // 获取角色信息
        UmsRole role = getRole(reqDTO);
        // 获取资源信息
        UmsResource resource = getResource(reqDTO);
        // 角色资源关系
        UmsRoleResourceRelation relation = getUmsRoleResourceRelation(role.getId(), resource.getId());
        if (ObjectUtils.isEmpty(relation)) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_NOT_EXIST);
        }
        return this.removeById(relation);
    }

    private UmsResource getResource(UmsRoleResourceRelationReqDTO reqDTO) {
        UmsResource resource = umsResourceService.getResource(reqDTO.getResourceCode());
        if (ObjectUtils.isEmpty(resource)) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_NOT_EXIST);
        }
        return resource;
    }

    private UmsRoleResourceRelation getUmsRoleResourceRelation(Long rid, Long sid) {
        return this.getOne(Wrappers.<UmsRoleResourceRelation>lambdaQuery().eq(UmsRoleResourceRelation::getRid, rid).eq(UmsRoleResourceRelation::getSid, sid));
    }

    private UmsRole getRole(UmsRoleResourceRelationReqDTO reqDTO) {
        UmsRole role = umsRoleService.getRole(reqDTO.getRoleCode());
        if (ObjectUtils.isEmpty(role)) {
            throw new BaseException(CodeMsg.UMS_ROLE_NOT_EXIST);
        }
        return role;
    }
}
